package com.wbt.bambootown.ui.activity;

import android.support.design.widget.TextInputEditText;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.entity.User;
import com.wbt.bambootown.mvp.contract.LoginContract;
import com.wbt.bambootown.mvp.frame.MvpActivity;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.LoginPresenter;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.Log;
import com.wbt.bambootown.utils.SPUtils;


import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends MvpActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.edit_sign_in_username)
    TextInputEditText mEditSignInUserName;
    @BindView(R.id.edit_sign_in_password)
    TextInputEditText mEditSignInPassword;


    private String userName = null;
    private String password = null;

    @OnClick(R.id.btn_sign_in)
    public void onViewClicked() {
        if (checkForm()) {
            mPresenter.login(userName, password);
            showProgressDialog();
        }
    }

    @OnClick(R.id.iv_setting)
    public void setting(){
        startActivity(ChangeAddressActivity.class);
    }


    private boolean checkForm() {
        userName = mEditSignInUserName.getText().toString().trim();
        password = mEditSignInPassword.getText().toString().trim();

        boolean isPass = true;

        if (userName.isEmpty()) {
            mEditSignInUserName.setError("用户名不能为空");
            isPass = false;
        } else {
            mEditSignInUserName.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mEditSignInPassword.setError("请填写至少6位密码");
            isPass = false;
        } else {
            mEditSignInPassword.setError(null);
        }

        return isPass;
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoginSuccess() {
        dismissProgressDialog();
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        SPUtils.saveUser(new User(userName, password));
        FrameApp.loadUser();
        startActivity(MainActivity.class);
        ActivityManagerUtil.create().finishActivity();
    }

    @Override

    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .transparentStatusBar()
                .init();
    }

    @Override
    public void initView() {
        FrameApp.loadUser();
        if(null != FrameApp.user){
            mEditSignInUserName.setText(FrameApp.user.getUsername());
            mEditSignInPassword.setText(FrameApp.user.getPassword());
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_login;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

}
